import { routerRedux } from 'dva/router';
import { stringify } from 'qs';
import {
  getNewsCategorys,
  getNewsDetails,
  getNewsDetails2,
  getNewsDetail,
} from '@/services/http';

export default {
  namespace: 'handbook',
  state: {
    categoryList: [],
    newsList:[],
    total: 0,
    isLoading: true,
    ret: {},
  },

  effects: {
    /**
     * 获取活动列表
     * @param payload
     * @param call
     * @param put
     * @returns {IterableIterator<*>}
     */
    *getNewsCategorys(payload, { call, put }) {
      const response = yield call(getNewsCategorys, payload.payload);
      yield put({
        type: '_getNewsCategorys',
        data: response,
      });
    },

    *getNewsDetails(payload, { call, put }) {
      const {callback} = payload.payload
      const response = yield call(getNewsDetails, payload.payload);
      callback(response.ret)
    },

    *getNewsDetails2(payload, { call, put }) {
      const {callback} = payload.payload
      const response = yield call(getNewsDetails2, payload.payload);
      callback(response.ret)
    },

    *getNewsDetail(payload, { call, put }) {
      const { callback } = payload.payload;
      const response = yield call(getNewsDetail, payload.payload);
      callback(response.ret)
    },

  },

  reducers: {
    _getNewsCategorys(state, action) {
      return {
        ...state,
        categoryList: action.data.code == 1 ? action.data.ret : [],
        total: action.data.code == 1 ? action.data.total : 0,
        isLoading: false,
      };
    },

    _getNewsDetails(state, action) {
      return {
        ...state,
        newsList: action.data.code == 1 ? action.data.ret : [],
        total: action.data.code == 1 ? action.data.total : 0,
        isLoading: false,
      };
    },
  },
};
